package com.itheima.service;

import com.itheima.config.AliyunOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author wuzhiyong
 */
@Service
public class UploadService {
    @Autowired
    private AliyunOssUtil aliyunOssUtil;


    public String upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        // 修改文件名称并保存文件
        String suffix = null;
        if (fileName != null) {
            suffix = fileName.substring(fileName.lastIndexOf("."));
        }
        //随机数 防止文件名称重复
        String uuid = UUID.randomUUID().toString();
        fileName = uuid + suffix;
        // 入阿里云
        String bucketName = "reggieoss";
        String endpoint = "oss-cn-guangzhou.aliyuncs.com";
        String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
        aliyunOssUtil.upload(file.getInputStream(), fileName);
        return url;
    }
}
